package layui.layui.controller;

import layui.layui.entity.Books;
import layui.layui.pojo.Response;
import layui.layui.service.BookService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/BookController")
public class BookController {
    @Autowired
    BookService bookService;

    @ResponseBody
    @GetMapping("/getBooks")
    public Response<Books> getBooks(@RequestParam(value = "key", required = false) String keywd){
        Response<Books> result = new Response<Books>();
        List<Books> temp = bookService.getBooks(keywd);
        result.setData(temp);
        result.setCount(temp.size());
        System.out.println(result);
        return result;
    }

    @GetMapping("/")
    String index(HttpServletRequest request){
        return "main.html";
    }

    @PostMapping("/deleteBooks")
    @ResponseBody
    public Response<?> deleteBooks(String ids) {
        bookService.deleteBooks(ids);
        return new Response<>();
    }

    @PostMapping("/editBookName")
    @ResponseBody
    public Response<?> editBookName(String name, int id) {
        bookService.editBookName(name, id);
        return new Response<>();
    }

    @PostMapping("/editBookCount")
    @ResponseBody
    public Response<?> editBookCount(int count, int id) {
        bookService.editCount(count, id);
        return new Response<>();
    }

    @GetMapping("/getName")
    @ResponseBody
    public Response<String> getName(int id) {
        String name = bookService.getName(id);
        return new Response<String>().addData(name);
    }

    @PostMapping("/addBorrow")
    @ResponseBody
    public Response<?> addBorrow(int stuId, int bookId) {
        bookService.addBorrow(stuId, bookId);
        return new Response<>();
    }
}
